package com.ycsj.goldmedalnewconcept.activity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.google.gson.Gson;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.picasso.Picasso;
import com.ycsj.goldmedalnewconcept.bean.CommentResponse;
import com.ycsj.goldmedalnewconcept.bean.SumCommentResp;
import com.ycsj.goldmedalnewconcept.bean.SumCommentResp.SumCommentlistDetail;
import com.ycsj.goldmedalnewconcept.constant.Constant;
import com.ycsj.goldmedalnewconcept.utils.SPUtil;
import com.ycsj.goldmedalnewconcept.R;

//评价老师(列表)
public class CommentActivity extends Activity {

	private ListView lv_judge;
	private ImageView iv_back;
	private ImageView iv_header;
	private TextView tv_name;
	private TextView tv_year;
	private TextView tv_judge;
	private List<SumCommentlistDetail> list = new ArrayList<SumCommentlistDetail>();
	private MyAdapter mAdapter;
	private TextView tv_empty;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_comment);
		initView();
		setOnListener();

	}

	@Override
	protected void onResume() {
		super.onResume();
		initData();
	}

	private void initData() {

		// OkHttpClient mOkHttpClient = new OkHttpClient();
		// // 创建一个Request
		// final Request request = new Request.Builder()
		// .url("http://192.168.0.198:8888/ycsj_platform/PersonalAssistantMsg?type=13&c=15098012693&role=1")
		// .build();
		// // new call13355556665
		// Call call = mOkHttpClient.newCall(request);
		// call.enqueue(callBack);

		OkHttpClient mOkHttpClient = new OkHttpClient();
		// mOkHttpClient.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
		FormEncodingBuilder builder = new FormEncodingBuilder();
		builder.add("type", "13");
		builder.add("c", SPUtil.getString(CommentActivity.this, "account", ""));
		builder.add("role", SPUtil.getString(CommentActivity.this, "role", ""));
		Request request = new Request.Builder()
				.url(Constant.SERVER_URL + "PersonalAssistantMsg")
				.post(builder.build()).build();
		mOkHttpClient.newCall(request).enqueue(callBack);

	}

	private void setOnListener() {
		iv_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	private void initView() {
		lv_judge = (ListView) findViewById(R.id.lv_judge);
		iv_back = (ImageView) findViewById(R.id.iv_back);
		tv_empty = (TextView) findViewById(R.id.tv_empty);
	}

	class MyAdapter extends BaseAdapter {

		private LayoutInflater mInflater = null;

		private MyAdapter(Context context) {
			this.mInflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			ViewHolder vh;
			if (convertView == null) {
				vh = new ViewHolder();
				convertView = mInflater.inflate(R.layout.list_comment, null);
				vh.tv_judge = (TextView) convertView
						.findViewById(R.id.tv_judge);
				vh.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
				vh.tv_sex = (TextView) convertView.findViewById(R.id.tv_sex);
				vh.tv_tel = (TextView) convertView.findViewById(R.id.tv_tel);
				vh.ratingBar = (RatingBar) convertView.findViewById(R.id.ratingBar);
				vh.iv_header = (ImageView) convertView
						.findViewById(R.id.iv_header);
				convertView.setTag(vh);
			} else {
				vh = (ViewHolder) convertView.getTag();
			}

			vh.tv_name.setText("姓名:"+list.get(position).NAME);
			vh.tv_tel.setText("电话:"+list.get(position).TEACHER_MOBILE);
			int score=list.get(position).SUM_SCORE;
			int count=list.get(position).COUNT;
			vh.ratingBar.setRating(score/count);
			if ("1".equals(list.get(position).SEX)) {

				vh.tv_sex.setText("性别:男♂");
			} else {
				vh.tv_sex.setText("性别:女 ♀");
			}
			Picasso.with(CommentActivity.this).load(list.get(position).ICON)
					.into(vh.iv_header);
			vh.tv_judge.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(CommentActivity.this,
							ParentsEvaluationActivity.class);
					String[] teacherinfo = { list.get(position).NAME,
							list.get(position).SEX, list.get(position).ICON,
							list.get(position).TEACHER_MOBILE };
					intent.putExtra("teacherinfo", teacherinfo);
					intent.putExtra("TEACHER_MOBILE",list.get(position).TEACHER_MOBILE);
					startActivity(intent);
				}
			});

			return convertView;
		}

		class ViewHolder {
			private TextView tv_judge;
			private TextView tv_name;
			private TextView tv_sex;
			private TextView tv_tel;
			private ImageView iv_header;
			private RatingBar ratingBar;
		}

	}

	Handler handler = new Handler();
	Callback callBack = new Callback() {

		@Override
		public void onFailure(Request arg0, IOException arg1) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onResponse(Response arg0) throws IOException {
			String json = arg0.body().string();
			Log.e("test", json);
			Gson gson = new Gson();
			list.removeAll(list);
			SumCommentResp fromJson = gson.fromJson(json,
					SumCommentResp.class);
			if(fromJson!=null){
				if("-1".equals(fromJson.state)){
					handler.postDelayed(new Runnable() {

						@Override
						public void run() {
							tv_empty.setVisibility(tv_empty.VISIBLE);
						}
					}, 0);
				}
			}
			
			
			if (!("{\"state\":-1}").equals(json)) {
				
				if (fromJson.list != null) {
					list.addAll(fromJson.list);
					handler.postDelayed(new Runnable() {

						@Override
						public void run() {

							mAdapter = new MyAdapter(CommentActivity.this);
							lv_judge.setAdapter(mAdapter);
						}
					}, 0);
				}
					
				
			}
		}
	};
}
